﻿
// **************************************************************************
//    Class Created by Mick Doherty (Dotnetrix) June 2010
//    http://dotnetrix.co.uk/
//
//    ...for GlassUI (an AeroGlass UI Library)
//    http://glassui.codeplex.com/
// **************************************************************************

using System.ComponentModel.Design;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Resources;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using GlassUI.Properties;

namespace GlassUI.Design
{
    internal class StackedPanelControlDesigner : ControlDesigner
    {
        public new StackedPanelControl Control
        {
            get
            {
                return base.Control as StackedPanelControl;
            }
        }

        public override System.Collections.ICollection AssociatedComponents
        {
            get
            {
                return ((StackedPanelControl)this.Control).StackedPanels;
            }
        }

        public override System.ComponentModel.Design.DesignerActionListCollection ActionLists
        {
            get
            {
                DesignerActionListCollection list = new DesignerActionListCollection();
                list.Add(new StackedPanelControlActionList(this));
                return list;
            }
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
        }

    }

    internal class StackedPanelControlActionList : DesignerActionList
    {
        #region Private Instance variables

        private IDesignerHost host;
        private ResourceManager rm;
        private DesignerActionUIService actionUIService;

        #endregion

        public StackedPanelControlActionList(ControlDesigner owner)
            : base(owner.Component)
        {
            this.host = base.GetService(typeof(IDesignerHost)) as IDesignerHost;
            this.rm = Resources.ResourceManager;
            this.actionUIService = base.GetService(typeof(DesignerActionUIService)) as DesignerActionUIService;
        }

        private string GetActionName(string action)
        {
            if (action == "Add")
                return rm.GetString("AddPanel", CultureInfo.CurrentUICulture);
            else if (action == "Remove")
                return rm.GetString("RemovePanel", CultureInfo.CurrentUICulture);
            return null;
        }

        public override DesignerActionItemCollection GetSortedActionItems()
        {
            DesignerActionItemCollection items = new DesignerActionItemCollection();
            if (this.GetActionName("Add") != null)
                items.Add(new DesignerActionMethodItem(this, "OnAddActionClick", this.GetActionName("Add"), true));
            if (this.GetActionName("Remove") != null)
                items.Add(new DesignerActionMethodItem(this, "OnRemoveActionClick", this.GetActionName("Remove"), true));
            return items;
        }

        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        private void OnAddActionClick()
        {
            if ((this.host != null))
            {
                StackedPanel panel;
                IComponentChangeService c = (IComponentChangeService)GetService(typeof(IComponentChangeService));
                using (DesignerTransaction dt = host.CreateTransaction(this.GetActionName("Add")))
                {
                    panel = (StackedPanel)host.CreateComponent(typeof(StackedPanel));
                    panel.Text = panel.Name;
                    c.OnComponentChanging(this.Component, null);
                    ((StackedPanelControl)this.Component).StackedPanels.Add(panel);
                    ((StackedPanelControl)this.Component).SelectedIndex = ((StackedPanelControl)this.Component).Controls.IndexOf(panel);
                    c.OnComponentChanged(this.Component, null, null, null);
                    dt.Commit();
                    actionUIService.Refresh(this.Component);
                }
            }
        }

        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        private void OnRemoveActionClick()
        {
            if ((this.host != null))
            {
                StackedPanelControl control = this.Component as StackedPanelControl;
                if (control.Controls.Count > 0)
                {
                    StackedPanel panel = control.SelectedIndex == -1 ? null : control.StackedPanels[control.SelectedIndex];
                    IComponentChangeService c = (IComponentChangeService)GetService(typeof(IComponentChangeService));
                    using (DesignerTransaction dt = host.CreateTransaction(this.GetActionName("Remove")))
                    {
                        if (panel != null)
                        {
                            c.OnComponentChanging(this.Component, null);
                            control.Controls.Remove(panel);
                            host.DestroyComponent(panel);
                            //control.SelectedIndex--;
                            c.OnComponentChanged(this.Component, null, null, null);
                            dt.Commit();
                        }
                    }
                }
            }
        }
    }

}
